package com.tio.showcase.client;

import com.tio.showcase.common.ShowcaseAsAioHandler;
import com.tio.showcase.common.ShowcasePacket;
import com.tio.showcase.common.Type;
import com.tio.showcase.handler.LoginRespHandler;
import com.tio.showcase.showcaseintf.AbsShowcaseBsHandler;
import org.tio.client.intf.ClientAioHandler;
import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;

import java.util.HashMap;
import java.util.Map;

/**
 * Author      : liyuemin
 * Time        : 2017/12/15 16:34
 * Description :
 */
public class ShowcaseAioClientHandler extends ShowcaseAsAioHandler implements ClientAioHandler {

    private static ShowcasePacket heartbeatPacket = new ShowcasePacket(Type.HEART_BEAT_REQ,null);

    private static Map<Byte,AbsShowcaseBsHandler<?>> handlerMap = new HashMap<Byte, AbsShowcaseBsHandler<?>>();
    static{
        handlerMap.put(Type.LOGIN_RESP,new LoginRespHandler());
    }

    @Override
    public void handler(Packet packet, ChannelContext channelContext) throws Exception {
        ShowcasePacket showcasePacket = (ShowcasePacket) packet;
        byte type = showcasePacket.getType();
        AbsShowcaseBsHandler<?> showcaseBsHandler = handlerMap.get(type);
        showcaseBsHandler.handler(showcasePacket,channelContext);
        return ;
    }

    public Packet heartbeatPacket() {
        return heartbeatPacket;
    }
}
